angular.module('PackingStorage')

.controller('PackingStorage.BatsCtrl', ['$scope', '$ionicHistory', '$stateParams', '$timeout', 'User', 'Device', 'imanDialog', 'PackingStorageHomeService', 'isSanner',
    function($scope, $ionicHistory, $stateParams, $timeout, User, Device, imanDialog, PackingStorageHomeService, isSanner) {
        var packCode = $stateParams.packData.substring(0, $stateParams.packData.indexOf("&"));
        var wareHouse = $stateParams.packData.substring($stateParams.packData.indexOf("&") + 1);
        var userId = User.get("_id"); //用户id
        var deviceId = Device.get("Terminal")._id;
        var deviceName = Device.get("Terminal").Name;
        var workId = Device.get("Work")._id;
        var workName = Device.get("Work").Name;
        var WorkLocationId = Device.get("WorkLocation")._id;
        var WorkLocationName = Device.get("WorkLocation").Name;

        $scope.data = {};
        $scope.count = 0;

        var barcodeMasters = [];

        $scope.submitData = function() {
            imanDialog.showLoading();
            PackingStorageHomeService.submitData(wareHouse, packCode, barcodeMasters, barcodeMasters[0].ItemMaster, deviceId, deviceName, workId, workName, WorkLocationId, WorkLocationName, userId).then(function(data) {
                if (data === true) {
                    imanDialog.hideLoading();
                    imanDialog.toastBottom("入库成功");
                    $ionicHistory.goBack();
                } else {
                    imanDialog.hideLoading();
                    imanDialog.toastBottom("入库失败");
                }
            }, function() {
                imanDialog.hideLoading();
            });
        };

        $scope.data.gridOptions = {
            enableRefresh: false,
            enableCheck: false,
            enableOperate: false,
            operateDefs: [
                { name: 'op1', icon: u9ImPDA.getFullPath('img/icon_list.png', 'Demo') },
                { name: 'op2', icon: u9ImPDA.getFullPath('img/icon_item.png', 'Demo') }
            ],
            columnDefs: [
                { col: 'Barcode', name: 'ID' },
                { col: 'ShipLotNumber', name: '批次' },
                { col: 'DescSeg1', name: '尺寸' },
                { col: 'ItemMasterCode', name: '型号' }
            ],
            data: barcodeMasters
        };

        $scope.$on('$ionicView.afterEnter', function() {
            isSanner.open();
        });
        $scope.$on('$ionicView.beforeLeave', function() {
            isSanner.close();
        });

        var scanning = false;
        $scope.$on('iScanner.DataReceived', function(e, data) {
            if (scanning) {
                return;
            }
            scanning = true;
            var barcode = data;
            var i = barcodeMasters.length - 1;
            for (; i >= 0; i--) {
                if (barcode === barcodeMasters[i].Barcode) {
                    break;
                }
            }
            if (i === -1) {
                getItemBarcode(barcode);
            } else {
                scanning = false;
                imanDialog.toastBottom("该条码已扫描");
            }
        });

        //扫描镭射标查找对应芯片的信息
        function getItemBarcode(barcode) {
            imanDialog.showLoading();
            PackingStorageHomeService.getItemBarcodeData(barcode).then(function(data) {
                if (data != null) {
                    barcodeMasters.push(data);
                    $scope.count++;
                } else {
                    imanDialog.toastBottom("该条码不存在");
                }
            }).finally(function() {
                scanning = false;
                imanDialog.hideLoading();
            });
        }
    }
]);
